package com.gopay.scheduler.service.alarm.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.dao.CheckNotfiyOrderAlarmDao;
import com.gopay.scheduler.service.alarm.CheckNotfiyOrderAlarmService;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.domain.RequestVO;

/**
 * 2016年11月25日 
 * www.gopay.com.cn Inc.All rights reserved.
 */

@Service("checkNotfiyOrderAlarmService")
public class CheckNotfiyOrderAlarmServiceImpl implements CheckNotfiyOrderAlarmService{

	
	@Value("#{webProperties['autoSettleRunEnvironment']}")
	private String env;
	
	private Logger logger = LoggerFactory.getLogger(CheckNotfiyOrderAlarmServiceImpl.class);
	
	@Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
	
	@Resource(name="checkNotfiyOrderAlarmDao")
	private CheckNotfiyOrderAlarmDao checkNotfiyOrderAlarmDao;
	
	@Override
	public int getNotNotfiyOrderCounts(Map<String,Object> map) {
		
		String SQL=buildSQL(map);
		
		List<Object[]> count =commonQueryDAO.findBySql(SQL, map);
		
		if(count!=null&&!count.isEmpty()&&count.get(0)!=null){
			
			if(count.get(0)[1]!=null){
				logger.info("CheckNotfiyOrderAlarmJob-counts is:【"+count.get(0)[1]+"】");
				BigDecimal counts= (BigDecimal) count.get(0)[1];
				return counts.intValue();
			}
			logger.info("CheckNotfiyOrderAlarmJob-count.get(0)[1]==null");
			return 0;
		}
		return 0;
	}

	private String buildSQL(Map<String, Object> map) {
		
		StringBuffer SQL = new StringBuffer();
		SQL.append("SELECT '1',COUNT(*)");
		SQL.append("  FROM CPS_GEN_MAIN_ORDER T");
		SQL.append("  LEFT JOIN NOTIFY_MER_ORDER_LOG N");
		SQL.append("    ON N.GOPAY_ORDER_ID = T.GOPAY_ORDER_ID");
		SQL.append("   WHERE 1=1");
		SQL.append("   AND N.SQ IS NULL");
		SQL.append("   AND T.TXN_STA_CD = 20000");
		SQL.append("   AND T.GOPAY_INT_TXN_CD = '00100'");
		SQL.append("   AND T.GOPAY_TXN_TM >=TO_DATE(:startDate, 'yyyy-MM-dd HH24:mi:ss')");
		SQL.append("   AND T.GOPAY_TXN_TM <= TO_DATE(:endDate, 'yyyy-MM-dd HH24:mi:ss')");
		SQL.append(" ORDER BY T.GOPAY_TXN_TM ");

		logger.info("CheckNotfiyOrderAlarmJob PARAMS:startDate="
				+ map.get("startDate") + ",endDate=" + map.get("endDate"));
		logger.info("CheckNotfiyOrderAlarmJob SQL:" + SQL.toString());

		return SQL.toString();
	}

	@Override
	public void sendMassageAlarm(int count,String phones) {
		
		this.logger.info("CheckNotfiyOrderAlarmJob-phones="+phones+",counts="+count);
		if(phones!=null&&count>0){
			RequestVO req = new RequestVO();
			req.setAppId(SMSAppId._90000.value);
			req.setMobile(phones);
			req.setContent("环境:"+env+",报警：00100交易未发送NOTFIY通知的订单数量为："+count);
			GopaySMSHttpClient.getInstance().createVerificationCode(req);
		}
		
	}
	
	
	
	public void insertNotfiyOrder(Map<String,Object> map){
		
		String SQL=buildInsertSQL();
		logger.info("CheckNotfiyOrderAlarmJob-SQL-I："+SQL);
		checkNotfiyOrderAlarmDao.updateBySql(SQL, map);
	}

	private String buildInsertSQL() {
		StringBuffer SQL = new StringBuffer();
		SQL.append("INSERT INTO NOTIFY_MER_ORDER_LOG");
		SQL.append("  (SQ,GOPAY_ORDER_ID,NOTIFY_DATA,MER_FRONT_URL,");
		SQL.append("   MER_SERVER_URL,NOTIFY_COUNT,IS_NOTIFY_OK,");
		SQL.append("   LAST_NOTIFY_TIME,NOTIFY_TYPE,REMARK1,REMARK2,");
		SQL.append("   REMARK3,SEND_STATUS,CREATE_CHL,CREATE_TIME,JUMP_URL)");
		SQL.append("  SELECT NOTIFY_MER_ORDER_LOG_SEQ.NEXTVAL,");
		SQL.append("         T.GOPAY_ORDER_ID,'',PG.MER_NOTIFY_FRONT_URL,PG.MER_NOTIFY_SERVER_URL,");
		SQL.append("         0,0,NULL,1,'101','00100','cron',0,0,SYSTIMESTAMP,''");
		SQL.append("    FROM CPS_GEN_MAIN_ORDER T");
		SQL.append("    LEFT JOIN NOTIFY_MER_ORDER_LOG N");
		SQL.append("      ON N.GOPAY_ORDER_ID = T.GOPAY_ORDER_ID");
		SQL.append("    LEFT JOIN PG_GEN_ORDER_TRANS_DTL PG");
		SQL.append("      ON PG.TRANS_DTL_SQ = T.TRANS_DTL_SQ");
		SQL.append("   WHERE 1 = 1");
		SQL.append("     AND N.SQ IS NULL");
		SQL.append("     AND T.TXN_STA_CD = 20000");
		SQL.append("     AND T.GOPAY_INT_TXN_CD = '00100'");
		SQL.append("     AND T.GOPAY_TXN_TM >=TO_DATE(:startDate, 'yyyy-MM-dd HH24:mi:ss')");
		SQL.append("     AND T.GOPAY_TXN_TM <= TO_DATE(:endDate, 'yyyy-MM-dd HH24:mi:ss')");
		return SQL.toString();
	}

}
